<template>
  <t-row :gutter="[16, 16]">
    <t-col v-for="(item, index) in PANE" :key="item.title" :xs="6" :xl="3">
      <t-card
        :title="t(item.title)"
        :bordered="false"
        :class="{ 'dashboard-item': true, 'dashboard-item--main-color': index == 0 }"
      >
        <div class="dashboard-item-top">
          <span :style="{ fontSize: `${resizeTime * 28}px` }">{{ item.number }}</span>
        </div>
        <div class="dashboard-item-left">
          <span v-if="index === 0" :style="{ marginTop: `-24px` }">
            <icon name="money" />
          </span>
          <span v-else-if="index === 1" :style="{ marginTop: `-24px` }">
            <icon name="saving-pot" />
          </span>
          <span v-else-if="index === 2" :style="{ marginTop: `-24px` }">
            <usergroup-icon />
          </span>
          <span v-else :style="{ marginTop: '-24px' }">
            <icon name="rocket" />
          </span>
        </div>
        <template #footer>
          <div class="dashboard-item-bottom" @click="handleClick">
            <div class="dashboard-item-block">
              {{ $t('pages.dashboardBase.topPanel.cardTips') }}
              <trend
                class="dashboard-item-trend"
                :type="item.upTrend ? 'up' : 'down'"
                :describe="item.upTrend || item.downTrend"
              />
            </div>
            <t-icon name="chevron-right" />
          </div>
        </template>
      </t-card>
    </t-col>
  </t-row>
</template>

<script lang="ts">
export default {
  name: 'DashboardBase',
};
</script>

<script setup lang="ts">
import axios from 'axios';
import { BarChart, LineChart } from 'echarts/charts';
import * as echarts from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { Icon, UsergroupIcon } from 'tdesign-icons-vue-next';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';

// 导入样式
import Trend from '@/components/trend/index.vue';
import { t } from '@/locales';
import { paneListType } from '@/pages/dashboard/base/topValue';

// import { changeChartsTheme } from '@/utils/color';
//
// import { constructInitDashboardDataset } from '../index';

echarts.use([LineChart, BarChart, CanvasRenderer]);

// const store = useSettingStore();
const resizeTime = ref(1);
const router = useRouter();
const dataLoading = ref(false);
const PANE = ref<paneListType[]>([]);

const handleClick = () => {
  router.push('/adminModule/ordersManage');
};
const fetchData = async () => {
  dataLoading.value = true;
  try {
    const revenue = await axios.get('/api/admin/get-total-revenue');
    const users = await axios.get('/api/admin/get-total-user');
    const order = await axios.get('/api/admin/get-total-order');
    const refund = await axios.get('/api/admin/get-total-refund');
    console.log(revenue, users, order, refund);
    PANE.value.push({
      title: '宠物商店总收入',
      number: `￥${String(revenue.data.data.total)}`,
      upTrend: '10.5%',
    });
    PANE.value.push({
      title: '宠物商店总退款',
      number: `￥${String(refund.data.data.total)}`,
      downTrend: '8.5%',
    });
    PANE.value.push({
      title: '目前注册用户（个）',
      number: `${String(users.data.data.total)}个`,
      upTrend: '10.5%',
    });
    PANE.value.push({
      title: '产生订单数目（个）',
      number: `${String(order.data.data.total)}个`,
      downTrend: '20.5%',
    });
  } catch (e) {
    console.log(e);
  } finally {
    dataLoading.value = false;
  }
};

onMounted(() => {
  fetchData();
});

// const { width, height } = useWindowSize();
// watch([width, height], () => {
//   updateContainer();
// });

// watch(
//   () => store.brandTheme,
//   () => {
//     changeChartsTheme([refundChart]);
//   },
// );

// watch(
//   () => store.mode,
//   () => {
//     [moneyChart, refundChart].forEach((item) => {
//       item.dispose();
//     });
//
//     renderCharts();
//   },
// );
</script>

<style lang="less" scoped>
.dashboard-item {
  padding: var(--td-comp-paddingTB-xl) var(--td-comp-paddingLR-xxl);

  :deep(.t-card__header) {
    padding: 0;
  }

  :deep(.t-card__footer) {
    padding: 0;
  }

  :deep(.t-card__title) {
    font: var(--td-font-body-medium);
    color: var(--td-text-color-secondary);
  }

  :deep(.t-card__body) {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    position: relative;
    padding: 0;
    margin-top: var(--td-comp-margin-s);
    margin-bottom: var(--td-comp-margin-xxl);
  }

  &:hover {
    cursor: pointer;
  }

  &-top {
    display: flex;
    flex-direction: row;
    align-items: flex-start;

    > span {
      display: inline-block;
      color: var(--td-text-color-primary);
      font-size: var(--td-font-size-headline-medium);
      line-height: var(--td-line-height-headline-medium);
    }
  }

  &-bottom {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;

    > .t-icon {
      cursor: pointer;
      font-size: var(--td-comp-size-xxxs);
    }
  }

  &-block {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--td-text-color-placeholder);
  }

  &-trend {
    margin-left: var(--td-comp-margin-s);
  }

  &-left {
    position: absolute;
    top: 0;
    right: 0;

    > span {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: var(--td-comp-size-xxxl);
      height: var(--td-comp-size-xxxl);
      background: var(--td-brand-color-light);
      border-radius: 50%;

      .t-icon {
        font-size: 24px;
        color: var(--td-brand-color);
      }
    }
  }

  //// 针对第一个卡片需要反色处理
  //&--main-color {
  //  background: var(--td-brand-color);
  //  color: var(--td-text-color-primary);
  //
  //  :deep(.t-card__title),
  //  .dashboard-item-top span,
  //  .dashboard-item-bottom {
  //    color: var(--td-text-color-anti);
  //  }
  //
  //  .dashboard-item-block {
  //    color: var(--td-text-color-anti);
  //    opacity: 0.6;
  //  }
  //
  //  .dashboard-item-bottom {
  //    color: var(--td-text-color-anti);
  //  }
  //}
}
</style>
